package util

import (
	"nspel/entity"
	"nspel/nspel"
	"strconv"
)

func GetAreasList(provinceId int64, cityId int64, areaName string, joinYear int64, pageSize int64, currentPage int64) (count int64, areas *[]entity.Area, err error) {
	area := new(entity.Area)
	ormEng := nspel.GetOrmEngine().QueryTable(area.TableName())
	hashKey := ""
	if provinceId != 0 {
		ormEng = ormEng.Filter("province_id", provinceId)
		hashKey += "provinceId[" + strconv.FormatInt(provinceId, 10) + "]."
	}
	if cityId != 0 {
		ormEng = ormEng.Filter("city_id", cityId)
		hashKey += "cityId[" + strconv.FormatInt(cityId, 10) + "]."
	}
	ormEng = ormEng.Filter("name__contains", areaName)
	hashKey += "name[\"" + areaName + "\"]."
	if joinYear != 0 {
		if joinYear == -1 {
			ormEng = ormEng.Filter("join_year__isnull", true)
			hashKey += "joinYear[null]."
		} else {
			ormEng = ormEng.Filter("join_year", joinYear)
			hashKey += "joinYear[" + strconv.FormatInt(joinYear, 10) + "]."
		}
	}
	count, err = area.SelectCountByProvinceIdCityIdNameJoinYear(&ormEng, hashKey+"count")
	if err != nil {
		return
	}
	hashKey += "pageSize[" + strconv.FormatInt(pageSize, 10) + "]."
	hashKey += "currentPage[" + strconv.FormatInt(currentPage, 10) + "]"
	offset := (currentPage - 1) * pageSize
	ormEng = ormEng.Limit(pageSize, offset)
	areas, err = area.SelectAllByProvinceIdCityIdNameJoinYear(&ormEng, hashKey)
	if err != nil {
		return
	}
	return
}

func GetProvinceCityName(provinceId int64, cityId int64) (string, error) {
	province := new(entity.Province)
	err := province.SelectAllById(provinceId)
	if err != nil {
		return "", err
	}
	city := new(entity.City)
	err = city.SelectAllById(cityId)
	if err != nil {
		return "", err
	}
	return province.Name + city.Name, nil
}

func UpdateAreaSchoolNum(areaId int64) error {
	school := new(entity.School)
	ormEng := nspel.GetOrmEngine().QueryTable(school.TableName()).Filter("experiment_area_id", areaId)
	hashKey := "experimentAreaId[" + strconv.FormatInt(areaId, 10) + "].count"
	experimentNum, err := school.CustomCount(&ormEng, hashKey)
	if err != nil {
		return err
	}
	ormEng = ormEng.Filter("is_standout", true)
	hashKey = "experimentAreaId[" + strconv.FormatInt(areaId, 10) + "].isStandout[true].count"
	standoutNum, err := school.CustomCount(&ormEng, hashKey)
	if err != nil {
		return err
	}
	area := new(entity.Area)
	err = area.SelectAllById(areaId)
	if err != nil {
		return err
	}
	area.AuthorizedSchoolNum = int64(experimentNum)
	area.StandoutSchoolNum = int64(standoutNum)
	err = area.Update("authorized_school_num", "standout_school_num")
	if err != nil {
		return err
	}
	nspel.RedisKeyDelete("area")
	return nil
}

func GetCityAreaName(provinceId int64, cityId int64, areaId int64) (string, error) {
	area := new(entity.Area)
	err := area.SelectAllById(areaId)
	if err != nil {
		return "", err
	}
	city := new(entity.City)
	err = city.SelectAllById(cityId)
	if err != nil {
		return "", err
	}
	if city.Name != "市辖区" {
		return city.Name + " " + area.Name, nil
	} else {
		province := new(entity.Province)
		err = province.SelectAllById(provinceId)
		if err != nil {
			return "", err
		}
		return province.Name + " " + area.Name, nil
	}
}
